package com.mingyizhudao.app.view.mine;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.dixiang.framework.common.QjResult;
import com.dixiang.framework.widget.LoadingDialog;
import com.google.gson.reflect.TypeToken;
import com.mingyizhudao.app.R;
import com.mingyizhudao.app.entity.ProvinceEntity;
import com.mingyizhudao.app.utils.CommonUtils;
import com.mingyizhudao.app.utils.Constants;
import com.mingyizhudao.app.view.MYBaseActivity;
import com.mingyizhudao.app.view.doctor.bean.DoctorsEntity;
import com.mingyizhudao.app.view.mine.bean.OrderItemEntity;
import com.mingyizhudao.app.view.recommend.bean.OrderResutEntity;

public class CommentSubmitActivity extends MYBaseActivity{
	private OrderItemEntity orderItemEntity;

	private TextView tv_doctor_name,order_no,tv_yuyue,tv_fuwufei;

	private RatingBar rb_1,rb_2;

	private EditText et_comment;

	private Button btn_commit;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_comment_submit);
		activity = this;
		initView();
		getOrderDetail();
	}

	@Override
	public void initHeaderView() {
		setTitleViewValue("评价");
		setTitleViewValue(0,0,R.color.white);
		setTopViewBg(R.color.actionbar_bg_color);
		initBackView(R.drawable.left_back_white_icon, 0, new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void initView(){

		tv_doctor_name = (TextView) findViewById(R.id.tv_doctor_name);
		order_no = (TextView) findViewById(R.id.order_no);
		tv_yuyue = (TextView) findViewById(R.id.tv_yuyue);
		tv_fuwufei = (TextView) findViewById(R.id.tv_fuwufei);
		rb_1 = (RatingBar) findViewById(R.id.rb_1);
		rb_2 = (RatingBar) findViewById(R.id.rb_2);
		et_comment = (EditText) findViewById(R.id.et_comment);
		btn_commit = (Button) findViewById(R.id.btn_commit);

		btn_commit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				doNet();


			}
		});
	}

	private void getOrderDetail(){

		orderItemEntity = (OrderItemEntity) getIntent().getSerializableExtra("orderItemEntity");
		LoadingDialog.getInstance(activity).show();
		String url = getIntent().getStringExtra(Constants.PARM_ACTION_URL)+CommonUtils.getTokenParam(activity);;

		getHttpResult(url, Constants.HTTP_INDEX.First, new TypeToken<QjResult<OrderItemEntity>>() {});
	}

	@Override
	public void onSuccess(QjResult result, String flg) {
		super.onSuccess(result, flg);

		if(flg.equals(Constants.HTTP_INDEX.First)){
			orderItemEntity = (OrderItemEntity)result.getResults();

			if(orderItemEntity != null){
				//				id = orderItemEntity.getId();
				//				setOrderDetail();

				String name = orderItemEntity.getExpertName();
				if (name!=null&&name.length()>0&&!"null".equals(name)) {
					tv_doctor_name.setText(name);
				}
				order_no.setText(orderItemEntity.getRefNo());
				tv_yuyue.setText(orderItemEntity.getDepositTotalAmount()+"元");
				tv_fuwufei.setText(""+orderItemEntity.getServiceTotalAmount()+"元");


			}else{
				Toast.makeText( activity, "获取数据失败！", 0 ).show();	
			}

		}else if(flg.equals(Constants.HTTP_INDEX.Second)){
			
			Toast.makeText( activity, "评价成功！", 0 ).show();	
			finish();
		}


	}

	@Override
	public void onCompleted(Exception e, String flg) {
		super.onCompleted(e, flg);
		LoadingDialog.getInstance(activity).dismiss();
	}



	private void doNet(){
		int xing1 =(int) rb_1.getRating();
		int xing2 =(int) rb_2.getRating();//手术效果

		if (xing1==0) {
			Toast.makeText( activity, "请为服务效率评分！", Toast.LENGTH_SHORT ).show();	
			return;
		}

		if (xing2==0) {
			Toast.makeText( activity, "请为手术效果评分！", Toast.LENGTH_SHORT ).show();	
			return;
		}

		String comment = et_comment.getText().toString().trim();
		if (!(comment!=null&&comment.length()>0)) {
			Toast.makeText( activity, "请填写评价！", Toast.LENGTH_SHORT ).show();	
			return;
		}

		if (orderItemEntity==null) {
			Toast.makeText( activity, "数据加载中！", Toast.LENGTH_SHORT ).show();	
			return;
		}

		String id = orderItemEntity.getId();

		String username = CommonUtils.getMobile(activity);
		String token = CommonUtils.getToken(activity);
		Map map = new HashMap<String, Object>();
		map.put("username", username);
		map.put("token", token);

		map.put("id", id);
		map.put("doctor_attitude", xing1);
		map.put("effect", xing2);
		map.put("comment_text", comment);
		map.put("key", "comment");

		String url = Constants.baseURL+"comment";
		postHttpResult(url, map, Constants.HTTP_INDEX.Second, new TypeToken<QjResult<HashMap<String, Object>>>() {});


	}



}
